﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Maticsoft.DBUtility;
using System.Data.SqlClient;

namespace ElingOA6_Admin_DSJCJ.DAL
{
    public partial class PeerReviewBeenVoter
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityNo"></param>
        /// <param name="beenVoterClsCd"></param>
        /// <returns></returns>
        public DataSet GetAll(string entityNo, string beenVoterClsCd) 
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select a.FPositionNo, a.FDeptNo, a.FEntityNo, a.FEmplNo, b.FEmpName, b.FSeq from t_HREmplPosition a, t_HREmpl b ");
            strSql.Append(" where a.FEmplNo = b.FEmplNo AND a.FEntityNo=@FEntityNo AND FIsMainPosition='Y' AND FEmpStatus <> '99' ");
            strSql.Append(" AND A.FEmplNo NOT IN (");
            strSql.Append(" select FBeenVoterEmplNo from dbo.v_PeerReviewBeenVoter");
            if (beenVoterClsCd != "")
            {
                strSql.Append(" where FBeenVoterClsCd <> '" + beenVoterClsCd + "'");
            }
            strSql.Append(" ) order by FSeq asc");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)};
            parameters[0].Value = entityNo;

            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityNo"></param>
        /// <returns></returns>
        public DataSet GetAllList(string strwhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from dbo.v_PeerReviewBeenVoter");
            if (strwhere != "")
            {
                strSql.Append(" where " + strwhere);
            }
            strSql.Append(" order by FDeptSeq,FSeq,FDutyNo");

            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityNo"></param>
        /// <returns></returns>
        public DataSet GetAllByClsCd(string entityNo, string beenVoterClsCd)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FEmpName,deptName,b.* from v_HRDeptEmpl a,dbo.t_PeerReviewBeenVoter b ");
            strSql.Append(" where a.FEmplNo=b.FBeenVoterEmplNo and a.FDeptNo=b.FBeenVoterDeptNo and a.FEntityNo=@FEntityNo  and FIsMainPosition='Y'");
            if (beenVoterClsCd != "") 
            {
                strSql.Append(" and FBeenVoterClsCd = '" + beenVoterClsCd + "'");
            }
            strSql.Append(" order by FDeptNo,FSeq,FDutyNo");

            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)};
            parameters[0].Value = entityNo;

            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 得到部门名称
        /// </summary>
        /// <param name="empNo"></param>
        /// <returns></returns>
        public string GetDeptNoByEmpNo(string empNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FDeptNo from v_HRDeptEmpl ");
            strSql.Append(" where FEmplNo=@FEmplNo  and FIsMainPosition='Y'");

            SqlParameter[] parameters = {
					new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15)};
            parameters[0].Value = empNo;
            return DbHelperSQL.GetString(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 事务模式 先把当前选中当前主题下的类别全部删除，再增加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public void Add(List<Model.PeerReviewBeenVoter> peerReviewBeenVotorModelList, string beenVoterClsCd, decimal peerReviewID)
        {
            StringBuilder strSql = new StringBuilder();
            CommandInfo cmds = new CommandInfo();
            List<CommandInfo> cmdList = new List<CommandInfo>();

            //删除当前主题下的类别全部删除
            strSql.Append("delete from dbo.t_PeerReviewBeenVoter ");
            strSql.Append(" where FBeenVoterClsCd=@FBeenVoterClsCd and FPeerReviewID=@FPeerReviewID ");
            SqlParameter[] parameters = {
					new SqlParameter("@FBeenVoterClsCd", SqlDbType.NVarChar,30),
					new SqlParameter("@FPeerReviewID", SqlDbType.Decimal,13)};
            parameters[0].Value = beenVoterClsCd;
            parameters[1].Value = peerReviewID;

            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);

            //添加数据
            foreach (Model.PeerReviewBeenVoter model in peerReviewBeenVotorModelList)
            {
                strSql = new StringBuilder();
                cmds = new CommandInfo();
                strSql.Append("insert into dbo.t_PeerReviewBeenVoter(");
                strSql.Append("FEntityNo,FBeenVoterEmplNo,FBeenVoterClsCd,FPeerReviewID,FBeenVoterDeptNo)");
                strSql.Append(" values (");
                strSql.Append("@FEntityNo,@FBeenVoterEmplNo,@FBeenVoterClsCd,@FPeerReviewID,@FBeenVoterDeptNo)");
                SqlParameter[] parameters1 = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FBeenVoterEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FBeenVoterClsCd", SqlDbType.NVarChar,30),
					new SqlParameter("@FPeerReviewID", SqlDbType.Decimal,13),
					new SqlParameter("@FBeenVoterDeptNo", SqlDbType.NVarChar,12)};
                parameters1[0].Value = model.FEntityNo;
                parameters1[1].Value = model.FBeenVoterEmplNo;
                parameters1[2].Value = model.FBeenVoterClsCd;
                parameters1[3].Value = model.FPeerReviewID;
                parameters1[4].Value = model.FBeenVoterDeptNo;

                cmds.CommandText = strSql.ToString();
                cmds.Parameters = parameters1;
                cmdList.Add(cmds);
            }


            DbHelperSQL.ExecuteSqlTranWithString(cmdList);        
        }

    }
}
